//
//  RestaurantViewController.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/5/18.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "RestaurantViewController.h"
#import "MedicalModel.h"
#import "RestaurantCollectionViewCell.h"
#import "TOWebViewController.h"

@interface RestaurantViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (weak, nonatomic) IBOutlet UIImageView *subTitleImageView;

@property (strong, nonatomic) NSMutableArray *modelAry;//datasource

@property (weak, nonatomic) IBOutlet UIImageView *headerImage;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerViewHeight;//deader高度

@property (weak, nonatomic) IBOutlet UICollectionView *restaurantTV;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;

@end

@implementation RestaurantViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.restaurantTV.dataSource = self;
    self.restaurantTV.delegate = self;
    
    [self showIndicator:@"正在加载数据，请稍候..."];
    
    [self getModelAryInfo];//请求数据
    
    [self screenAdaptation];//适配
    
    [self.restaurantTV registerClass:[RestaurantCollectionViewCell class] forCellWithReuseIdentifier:@"restaurantCell"];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark -- 屏幕适配
- (void)screenAdaptation{
    //Header 适配
    self.headerViewHeight.constant = MAIN_WIDTH / 375 * 200;
    
    
}

#pragma mark -- collectionViewDelegate & dataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.modelAry.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"restaurantCell";
    
    RestaurantCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
//    if (cell == nil) {
//        cell = [[[NSBundle mainBundle] loadNibNamed:@"RestaurantCollectionViewCell" owner:self options:nil] lastObject];
//    }
    
    cell.model = self.modelAry[indexPath.row];

    return cell;
}
//头部显示的内容
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//
//    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
//                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath];
//
//    [headerView addSubview:_headerView];//头部广告栏
//    return headerView;
//}
#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (MAIN_WIDTH - 30) / 2;
    return CGSizeMake(width, width / 345 * 300);
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 10;
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


#pragma mark -- 请求网络

- (void)getModelAryInfo{
    
    __weak __typeof(self) weakSelf = self;
    [HTTPManager get:[NSString stringWithFormat:@"%@/Api/test/index?tid=26&id=%@",urlHeader,self.urlId] success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self.modelAry removeAllObjects];
        
        for (NSDictionary *aDic in responseObject[@"date"][@"date"]) {
            
            MedicalModel *model = [[MedicalModel alloc] init];
            [model setValuesForKeysWithDictionary:responseObject];
            [model setValuesForKeysWithDictionary:responseObject[@"date"]];
            [model setValuesForKeysWithDictionary:aDic];
            
            [self.modelAry addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSLog(@"%@",[self.modelAry[0] top_pic]);
            [self.headerImage sd_setImageWithURL:[NSURL URLWithString:[self.modelAry[0] top_pic]] placeholderImage:[UIImage imageNamed:@"顶部占位图.png"]];
            
            [weakSelf updateTitle];
            
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [weakSelf hideIndicator];
    }];
    
}

#pragma mark -- 更新VC 标题 副标题
- (void)updateTitle{
    

    self.titleLabel.text = [self.modelAry[0] name];
    self.subTitleLabel.text = [self.modelAry[0] title];
    self.subTitleImageView.image = [UIImage imageNamed:@"美食特惠套餐.png"];
    
    [self hideIndicator];
    [self.restaurantTV reloadData];
    
    
}


#pragma mark -- Navigation返回Btn Click
- (IBAction)navigationReturnBtn:(id)sender {

    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark -- 懒加载
- (NSMutableArray *)modelAry{
    if (_modelAry == nil) {
        _modelAry = [NSMutableArray new];
    }
    return _modelAry;
}

#pragma mark -- 美味不用等
- (IBAction)deliciousBtn:(id)sender{ 
    
    TOWebViewController *webViewController = [[TOWebViewController alloc] initWithURL:[NSURL URLWithString:[((MedicalModel *)self.modelAry[0]) url]]];
     self.hidesBottomBarWhenPushed = YES;
    webViewController.title = [self.modelAry[0] name];
//    self.hidesBottomBarWhenPushed = YES;
//    self.navigationController.navigationBar.hidden = NO;
//    [self.navigationController presentViewController:webViewController animated:YES completion:nil];
    [self.navigationController pushViewController:webViewController animated:YES];
//    self.navigationController.navigationBar.hidden = YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
